package com.jwtproject.bootserver.security.handler;

import com.jwtproject.bootserver.status.ProjectStatusEnum;
import com.jwtproject.bootserver.util.UtilResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description：自定义认证失败处理类
 **/
@Slf4j
@Component
public class SecurityAuthenticationEntryPointHandler implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {

        e.printStackTrace();
        StringBuffer msg = new StringBuffer("请求访问：");
        msg.append(request.getRequestURI()).append(" 接口， 认证异常，请重新登录！");
        log.info(msg.toString());
        UtilResponse.writeFailureResult(response, ProjectStatusEnum.TOKEN_AUTH_FAILED);


//        if (e instanceof TokenExpiredException) {
//            StringBuffer msg = new StringBuffer("请求访问：");
//            msg.append(request.getRequestURI()).append(" 接口， 登录过期，请重新登录！");
//            log.info(msg.toString());
//            UtilResponse.writeFailureResult(response, ProjectStatusEnum.LOGIN_OUT_OF_DATE.code, YTSystemStatusEnum.LOGIN_OUT_OF_DATE.desc);
//        } else {
//            e.printStackTrace();
//            StringBuffer msg = new StringBuffer("请求访问：");
//            msg.append(request.getRequestURI()).append(" 接口， 认证异常，请重新登录！");
//            log.info(msg.toString());
//            UtilResponse.writeFailureResult(response, ProjectStatusEnum.TOKEN_AUTH_FAILED.code, YTSystemStatusEnum.TOKEN_AUTH_FAILED.desc);
//        }

    }


}
